# delimit ;

clear ;
clear matrix;


set mem 800m;

*add your path here:;
cd "C:\Users\jamesCabral\OneDrive - DAZ\CGH_Empirical_Replication";


*******************************;
* Run year-specific .do files *;
*******************************;
cd "data/ATUS_AHTUS_raw_data";

foreach yr in 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19{;
	cd "20`yr'/Basic-Transformed-Data-Files";
	do ../Data-Creation/format_`yr'.do;
	cd ../..;
};

**********************************;
*merge ATUS subsets for all years*;
**********************************;

cd "ATUS_All";

use 03_subset, clear;

append using 04_subset ;

append using 05_subset ;

append using 06_subset ;

append using 07_subset ;

append using 08_subset ;

append using 09_subset ;

append using 10_subset ;

append using 11_subset ;

append using 12_subset ;

append using 13_subset ;

append using 14_subset ;

append using 15_subset ;

append using 16_subset ;

append using 17_subset ;

append using 18_subset ;

append using 19_subset ;

*generate a quarter variable;
generate quarter=.;
* 2003;
replace quarter=1 if interview_date >= td(01jan2003) & interview_date<= td(31mar2003);
replace quarter=2 if interview_date >= td(01apr2003) & interview_date<= td(30jun2003);
replace quarter=3 if interview_date >= td(01jul2003) & interview_date<= td(30sep2003);
replace quarter=4 if interview_date >= td(01oct2003) & interview_date<= td(31dec2003);
* 2004;
replace quarter=1 if interview_date >= td(01jan2004) & interview_date<= td(31mar2004);
replace quarter=2 if interview_date >= td(01apr2004) & interview_date<= td(30jun2004);
replace quarter=3 if interview_date >= td(01jul2004) & interview_date<= td(30sep2004);
replace quarter=4 if interview_date >= td(01oct2004) & interview_date<= td(31dec2004);
* 2005;
replace quarter=1 if interview_date >= td(01jan2005) & interview_date<= td(31mar2005);
replace quarter=2 if interview_date >= td(01apr2005) & interview_date<= td(30jun2005);
replace quarter=3 if interview_date >= td(01jul2005) & interview_date<= td(30sep2005);
replace quarter=4 if interview_date >= td(01oct2005) & interview_date<= td(31dec2005);
* 2006;
replace quarter=1 if interview_date >= td(01jan2006) & interview_date<= td(31mar2006);
replace quarter=2 if interview_date >= td(01apr2006) & interview_date<= td(30jun2006);
replace quarter=3 if interview_date >= td(01jul2006) & interview_date<= td(30sep2006);
replace quarter=4 if interview_date >= td(01oct2006) & interview_date<= td(31dec2006);
* 2007;
replace quarter=1 if interview_date >= td(01jan2007) & interview_date<= td(31mar2007);
replace quarter=2 if interview_date >= td(01apr2007) & interview_date<= td(30jun2007);
replace quarter=3 if interview_date >= td(01jul2007) & interview_date<= td(30sep2007);
replace quarter=4 if interview_date >= td(01oct2007) & interview_date<= td(31dec2007);
* 2008;
replace quarter=1 if interview_date >= td(01jan2008) & interview_date<= td(31mar2008);
replace quarter=2 if interview_date >= td(01apr2008) & interview_date<= td(30jun2008);
replace quarter=3 if interview_date >= td(01jul2008) & interview_date<= td(30sep2008);
replace quarter=4 if interview_date >= td(01oct2008) & interview_date<= td(31dec2008);
* 2009;
replace quarter=1 if interview_date >= td(01jan2009) & interview_date<= td(31mar2009);
replace quarter=2 if interview_date >= td(01apr2009) & interview_date<= td(30jun2009);
replace quarter=3 if interview_date >= td(01jul2009) & interview_date<= td(30sep2009);
replace quarter=4 if interview_date >= td(01oct2009) & interview_date<= td(31dec2009);
* 2010;
replace quarter=1 if interview_date >= td(01jan2010) & interview_date<= td(31mar2010);
replace quarter=2 if interview_date >= td(01apr2010) & interview_date<= td(30jun2010);
replace quarter=3 if interview_date >= td(01jul2010) & interview_date<= td(30sep2010);
replace quarter=4 if interview_date >= td(01oct2010) & interview_date<= td(31dec2010);
* 2011;
replace quarter=1 if interview_date >= td(01jan2011) & interview_date<= td(31mar2011);
replace quarter=2 if interview_date >= td(01apr2011) & interview_date<= td(30jun2011);
replace quarter=3 if interview_date >= td(01jul2011) & interview_date<= td(30sep2011);
replace quarter=4 if interview_date >= td(01oct2011) & interview_date<= td(31dec2011);
* 2012;
replace quarter=1 if interview_date >= td(01jan2012) & interview_date<= td(31mar2012);
replace quarter=2 if interview_date >= td(01apr2012) & interview_date<= td(30jun2012);
replace quarter=3 if interview_date >= td(01jul2012) & interview_date<= td(30sep2012);
replace quarter=4 if interview_date >= td(01oct2012) & interview_date<= td(31dec2012);
* 2013;
replace quarter=1 if interview_date >= td(01jan2013) & interview_date<= td(31mar2013);
replace quarter=2 if interview_date >= td(01apr2013) & interview_date<= td(30jun2013);
replace quarter=3 if interview_date >= td(01jul2013) & interview_date<= td(30sep2013);
replace quarter=4 if interview_date >= td(01oct2013) & interview_date<= td(31dec2013);
* 2014;
replace quarter=1 if interview_date >= td(01jan2014) & interview_date<= td(31mar2014);
replace quarter=2 if interview_date >= td(01apr2014) & interview_date<= td(30jun2014);
replace quarter=3 if interview_date >= td(01jul2014) & interview_date<= td(30sep2014);
replace quarter=4 if interview_date >= td(01oct2014) & interview_date<= td(31dec2014);
* 2015;
replace quarter=1 if interview_date >= td(01jan2015) & interview_date<= td(31mar2015);
replace quarter=2 if interview_date >= td(01apr2015) & interview_date<= td(30jun2015);
replace quarter=3 if interview_date >= td(01jul2015) & interview_date<= td(30sep2015);
replace quarter=4 if interview_date >= td(01oct2015) & interview_date<= td(31dec2015);
* 2016;
replace quarter=1 if interview_date >= td(01jan2016) & interview_date<= td(31mar2016);
replace quarter=2 if interview_date >= td(01apr2016) & interview_date<= td(30jun2016);
replace quarter=3 if interview_date >= td(01jul2016) & interview_date<= td(30sep2016);
replace quarter=4 if interview_date >= td(01oct2016) & interview_date<= td(31dec2016);
* 2017;
replace quarter=1 if interview_date >= td(01jan2017) & interview_date<= td(31mar2017);
replace quarter=2 if interview_date >= td(01apr2017) & interview_date<= td(30jun2017);
replace quarter=3 if interview_date >= td(01jul2017) & interview_date<= td(30sep2017);
replace quarter=4 if interview_date >= td(01oct2017) & interview_date<= td(31dec2017);
* 2018;
replace quarter=1 if interview_date >= td(01jan2018) & interview_date<= td(31mar2018);
replace quarter=2 if interview_date >= td(01apr2018) & interview_date<= td(30jun2018);
replace quarter=3 if interview_date >= td(01jul2018) & interview_date<= td(30sep2018);
replace quarter=4 if interview_date >= td(01oct2018) & interview_date<= td(31dec2018);
* 2019;
replace quarter=1 if interview_date >= td(01jan2019) & interview_date<= td(31mar2019);
replace quarter=2 if interview_date >= td(01apr2019) & interview_date<= td(30jun2019);
replace quarter=3 if interview_date >= td(01jul2019) & interview_date<= td(30sep2019);
replace quarter=4 if interview_date >= td(01oct2019) & interview_date<= td(31dec2019);

*generate income bracket variables;
generate incom_bracket =.;
replace  incom_bracket=10 if missing(hh_income_03) ;
replace  incom_bracket=1  if hh_income_03>= 0       & hh_income_03<= 961.52;
replace  incom_bracket=2  if hh_income_03>961.52    & hh_income_03<= 1923.06;
replace  incom_bracket=3  if hh_income_03>1923.06   & hh_income_03<= 2884.60 ;
replace  incom_bracket=4  if hh_income_03>2884.60   & ! missing(hh_income_03); 

generate inc =.;
replace  inc = 10 if missing(hh_income_03);
replace  inc = 1  if hh_income_03>= 0       & hh_income_03<=599.9808 ;
replace  inc = 2  if hh_income_03> 599.9808 & hh_income_03<=1099.9808;
replace  inc = 3  if hh_income_03>1099.9808 & ! missing(hh_income_03);

save "ATUS_All_FINAL.dta", replace;